package excel

import (
	"fmt"

	"github.com/pkg/errors"
	"github.com/xuri/excelize/v2"
)

func readPromoMarketFile(filePath string) (dbRecords []*PromoMarket, err error) {
	f, err := excelize.OpenFile(filePath)
	if err != nil {
		println(err.Error())
		return
	}
	rows, err := f.GetRows("sheet1")
	if err != nil {
		return
	}
	for i := 1; i < len(rows); i++ {
		var cur = new(PromoMarket)
		cur.SalesPromoMarketID = rows[i][0]
		cur.PromoID = rows[i][1]
		cur.Locale = rows[i][2]
		dbRecords = append(dbRecords, cur)
	}
	return
}

func readSalesPromoMFile(filePath string) (dbRecords []*SalesPromo, err error) {
	f, err := excelize.OpenFile(filePath)
	if err != nil {
		println(err.Error())
		return
	}
	rows, err := f.GetRows("sheet1")
	if err != nil {
		return
	}
	for i := 1; i < len(rows); i++ {
		var cur = new(SalesPromo)
		cur.PromoID = rows[i][0]
		cur.StartTime = rows[i][3]
		cur.EndTime = rows[i][4]
		dbRecords = append(dbRecords, cur)
	}
	return
}

func readTimeConfigFile(filePath string) (timeConfigs []*TimeConfig, err error) {
	f, err := excelize.OpenFile(filePath)
	if err != nil {
		println(err.Error())
		return
	}
	rows, err := f.GetRows("sheet1")
	if err != nil {
		return
	}
	for i := 1; i < len(rows); i++ {
		var cur = new(TimeConfig)
		cur.Id = rows[i][0]
		cur.CampaignId = rows[i][1]
		cur.Locale = rows[i][2]
		timeConfigs = append(timeConfigs, cur)
	}
	return
}

var SheetName = "Sheet1"

func WritePromoMarketToFile(promoMarkets []*PromoMarket, fileName string) error {
	if len(promoMarkets) <= 0 {
		return errors.New("promoMarkets is null")
	}

	f := excelize.NewFile()
	// 创建一个工作表
	index := f.NewSheet(SheetName)
	// 设置表头
	initTitle(f)
	// 单元格行号
	var idx = 2
	// 遍历每一个参与者
	for _, curPromo := range promoMarkets {
		setLineValue(f, idx, curPromo)
		idx++

	}

	// 设置工作簿的默认工作表
	f.SetActiveSheet(index)
	// 根据指定路径保存文件
	if err := f.SaveAs(fileName); err != nil {
		return err
	}
	return nil
}

func initTitle(f *excelize.File) {
	f.SetCellValue(SheetName, "A1", "SalesPromoMarket_ID")
	f.SetCellValue(SheetName, "B1", "Promo_ID")
	f.SetCellValue(SheetName, "C1", "locale")
	f.SetCellValue(SheetName, "D1", "Start_Time")
	f.SetCellValue(SheetName, "E1", "End_Time")
}

func setLineValue(f *excelize.File, idx int, promoMarket *PromoMarket) error {
	f.SetCellValue(SheetName, fmt.Sprintf("%s%d", "A", idx), promoMarket.SalesPromoMarketID)
	f.SetCellValue(SheetName, fmt.Sprintf("%s%d", "B", idx), promoMarket.PromoID)
	f.SetCellValue(SheetName, fmt.Sprintf("%s%d", "C", idx), promoMarket.Locale)
	f.SetCellValue(SheetName, fmt.Sprintf("%s%d", "d", idx), promoMarket.StartTime)
	f.SetCellValue(SheetName, fmt.Sprintf("%s%d", "e", idx), promoMarket.EndTime)
	return nil
}

type TimeConfig struct {
	Id         string `json:"id"`
	CampaignId string `json:"campaign_id"`
	Locale     string `json:"Locale"`
}

type PromoMarket struct {
	SalesPromoMarketID string `json:"SalesPromoMarket_ID"`
	PromoID            string `json:"Promo_ID"`
	Locale             string `json:"locale"`
	StartTime          string `json:"Start_Time"`
	EndTime            string `json:"End_Time"`
}

type SalesPromo struct {
	PromoID   string `json:"Promo_ID"`
	StartTime string `json:"Start_Time"`
	EndTime   string `json:"End_Time"`
}
